package com.train.redis.core.generator;

import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.cache.interceptor.SimpleKey;

import java.lang.reflect.Method;

/**
 * @author jie
 * @Description:
 * @date 2020/1/6 17:04
 */
public class MyKeyGenerator implements KeyGenerator {

    public static final String KEY_GENERATOR_NAME = "keyGenerator";
 
    @Override
    public Object generate(Object target, Method method, Object... params) {
        if (params.length == 0) {
            return SimpleKey.EMPTY;
        }
        // 参数为自定义key=类名+方法名+属性的tostring字符串
        StringBuilder builder = new StringBuilder();
        // 分隔符
        String divide = ":";
        builder.append(target.getClass().getSimpleName()).append(divide);
        builder.append(method.getName());
        for (Object param : params) {
            builder.append(divide).append(param.hashCode());
        }
        return builder.toString();
    }

}